package com.financing.platform.web.api;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.dto.FpHdFinancingBlindDTO;
import com.financing.platform.service.IFpHdFinancingBlindService;
import com.financing.platform.util.DTOdateSet;

@Controller("fpHdFinancingBlindController")
@RequestMapping("/api/fpHdFinancingBlind")
public class FpHdFinancingBlindController extends BaseAction  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IFpHdFinancingBlindService fpHdFinancingBlindService;
	
	@RequestMapping("/listFpHdFinancingBlind.shtml")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FpHdFinancingBlindDTO fpHdFinancingBlind,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query fpHdFinancingBlind list begin .......");
			
			fpHdFinancingBlind.setBegin(pager.getStartIndex());
			fpHdFinancingBlind.setPageSize(pager.getPageSize());
			
			//过滤删除的视频
			fpHdFinancingBlind.setDeleteTag("1");
			
			int count = fpHdFinancingBlindService.queryListCount(fpHdFinancingBlind);
			
			pager.setTotalCount(count);
			
			List<FpHdFinancingBlindDTO> fpHdFinancingBlindList = null;
			
			if(count > 0){
				fpHdFinancingBlindList = fpHdFinancingBlindService.queryList(fpHdFinancingBlind);
			}else{
				fpHdFinancingBlindList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", fpHdFinancingBlindList);
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query fpHdFinancingBlind list end .......");
		}catch(Exception e){
			log.error("query fpHdFinancingBlind error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateFpHdFinancingBlind.shtml")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, FpHdFinancingBlindDTO fpHdFinancingBlind){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update fpHdFinancingBlind  begin .......");
			
			
			Map<String, Object>  map = getParameterMap(request);	
			DTOdateSet.setUpdateInfo(fpHdFinancingBlind,request.getSession());
			map.put("updateBy", fpHdFinancingBlind.getUpdateBy());
			map.put("updateDate", fpHdFinancingBlind.getUpdateDate());
			
			Integer id = fpHdFinancingBlind.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSet.setUpdateInfo(fpHdFinancingBlind,request.getSession());
			
			fpHdFinancingBlindService.update(map);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update fpHdFinancingBlind list end .......");
		}catch(Exception e){
			log.error("update fpHdFinancingBlind error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	/**
	 * 从request中获得参数Map，并返回可读的Map
	 * @param request
	 * @return
	 */
	@SuppressWarnings({ "rawtypes" })
	public  Map<String, Object> getParameterMap(HttpServletRequest request) {
	    // 参数Map
	    Map properties = request.getParameterMap();
	    // 返回值Map
	    Map<String,Object> returnMap = new HashMap<String,Object> ();
	    Iterator entries = properties.entrySet().iterator();
	    Map.Entry entry;
	    String name = "";
	    String value = "";
	    while (entries.hasNext()) {
	        entry = (Map.Entry) entries.next();
	        name = (String) entry.getKey();
	        Object valueObj = entry.getValue();
	        if(null == valueObj){
	            value = "";
	        }else if(valueObj instanceof String[]){
	            String[] values = (String[])valueObj;
	            for(int i=0;i<values.length;i++){
	                value = values[i] + ",";
	            }
	            value = value.substring(0, value.length()-1);
	        }else{
	            value = valueObj.toString();
	        }
	        returnMap.put(name, value);
	    }
	    return returnMap;
	}
}
